<?php

class Core_Backend_Component_Filebrowser_Controller_Index extends Dcmp_Component_Controller {

    public function indexAction($path, $filetype = '*', $files = array(), $callback = NULL, $title = '') {
        Kd_Util_IO::makeDirectory(BP . $path);
        $result = Kd_Util_IO::getDirectories(BP . $path, TRUE);
        return $this->viewResult(array('title'=>$title, 'directories' => $result, 'files'=>empty($files)? array(): explode(',', $files), 'root'=>$path, 'callback'=>$callback));
    }

    public function directoriesAction($path){
        $result = Kd_Util_IO::getDirectories(BP . $path, TRUE);
        $rootName = explode('/', $path);
        $rootName = end($rootName);
        return $this->templateResult(array('directories' => $result, 'root_path'=>$path, 'root_name'=>$rootName));
    }

    public function filesAction($path, $type = '*') {
        $result = Kd_Util_IO::getFiles((BP . $path), $type);
        return $this->templateResult(array('files' => $result));
    }

    public function createDirectoryAction($path, $name){
        Kd_Util_IO::makeDirectory($path.DS.$name);
        return $this->jsonResult(array('success'=>1));
    }

    public function uploadAction($path, $type = '*'){
        $file = $this->getRequest()->getFile('file');
        $filename = $file->saveTo(BP.$path);
        $pathinfo = pathinfo($filename);
        $pathinfo['url'] = '/'.str_replace(DS, '/', str_replace(BP, '', $filename));
        return $this->jsonResult($pathinfo);
    }
}